package com.woniu.apartment.ache;

import com.ruoyi.common.constant.ShaoxiaConstants;
import com.woniu.apartment.domain.ApartmentMain;
import com.woniu.apartment.domain.Comments;
import com.woniu.apartment.mapper.ApartmentMainMapper;
import com.woniu.apartment.mapper.CommentsMapper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shaox
 * @date 2023/4/6
 * @apiNote
 */

@Component
public class CommentAche implements InitializingBean {
    @Autowired
    private CommentsMapper commentsMapper;
    @Autowired
    private ApartmentMainMapper apartmentMainMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public void afterPropertiesSet() throws Exception {
        //启动时把评论存到redis中
        List<ApartmentMain> apartmentMainList=apartmentMainMapper.selectAll();
        for (ApartmentMain apartmentMain : apartmentMainList) {
            List<Comments> commentsList = commentsMapper.selectCommentsByApartId(apartmentMain.getId());
            Map<String, Comments> map = new HashMap<>();
            for (Comments comments : commentsList) {
                map.put(comments.getCustomerId()+"",comments);
            }
            redisTemplate.opsForHash().putAll(ShaoxiaConstants.APARTMENT_COMMENT+apartmentMain.getId()+"",map);
        }

    }
}

